
var lookProductObj = {

    /**
     * 描述 : 更换匹配的单词
     * 作者 : Zoro.Zhu
     */
    'lookProduct': function () {
        var keyword = window.prompt('请输入相关关键词', '');
        if (keyword === null || keyword === undefined || keyword === '') {
            return false;
        }
        var id = $('#id').val();
        var account = $('#account').val();
        window.open(ROOT_URL + '/?c=ctrl_wish_wishProduct&a=lookKeywordView&account=' + account + '&id=' + id + '&keyword=' + keyword);
    },

    /**
     * 描述 : 关键词查询工具
     * 作者 : Zoro.Zhu
     */
    'searchKeyword': function () {
        var keyword = encodeURIComponent($('#keyword').val());
        if (keyword === null || keyword === undefined || keyword === '') {
            return false;
        }
        window.location.href = ROOT_URL + '/?c=ctrl_wish_wishProduct&a=lookKeyword&k=' + keyword
    },

    /**
     * 描述 : 提交关键词的修改
     * 作者 : Zoro.Zhu
     */
    'updateKeyword': function () {
        let ids = [];
        $("input[name='check[]']:checked").each(function () {
            ids.push($(this).val())
        });
        if (ids.length < 1) {
            layer.msg("请先勾选关键词", {icon: 0});
            return false;
        }
        if (ids.length > 10) {
            layer.msg("最多允许勾选10个关键词", {icon: 0});
            return false;
        }
        var id = $('#id').val();
        var account = $('#account').val();
        $.ajax({
            url: ROOT_URL + "/?c=ctrl_wish_wishProduct&a=updateKeyword",
            type: "POST",
            data: {id: id, account: account, keywords: ids},
            success: function (result) {
                result = JSON.parse(result);
                if (result.state !== 200) {
                    layer.alert(result.info);
                    return false;
                } else {
                    layer.alert(result.info);
                    // parent.layer.closeAll();
                    return false;
                }
            }
        });
    },

    /**
     * 描述 : 修改标题
     * 作者 : Zoro.Zhu
     */
    'updateTitle' : function () {
        var title = window.prompt('请输入新的标题', '');
        if (title === null || title === undefined || title === '') {
            return false;
        }
        var id = $('#id').val();
        var account = $('#account').val();
        $.ajax({
            url: ROOT_URL + "/?c=ctrl_wish_wishProduct&a=updateTitle",
            type: "POST",
            data: {id: id, account: account, title: title},
            success: function (result) {
                result = JSON.parse(result);
                if (result.state !== 200) {
                    layer.alert(result.info);
                    return false;
                } else {
                    layer.alert(result.info);
                    // parent.layer.closeAll();
                    return false;
                }
            }
        });
    },

    /**
     * 描述 : 判断勾选了几个关键词
     * 作者 : Zoro.Zhu
     */
    'keywordNumber': function () {
        let ids = [];
        $("input[name='check[]']:checked").each(function () {
            ids.push($(this).val())
        });
        layer.msg("已勾选" + ids.length + "个关键词", {icon: 1});
    },

    /**
     * 描述 : 表格的点击事件，勾选复选框
     * 作者 : Zoro.Zhu
     */
    'onclickTr': function (obj) {
        // 找到checkbox对象
        var chks = $("input[type='checkbox']",obj);
        var tag = $(obj).attr("tag");
        if(tag == "selected"){
            // 之前已选中，设置为未选中
            $(obj).attr("tag","");
            chks.prop("checked",false);
        }else{
            // 之前未选中，设置为选中
            $(obj).attr("tag","selected");
            chks.prop("checked",true);
        }
        lookProductObj.keywordNumber();
    },

    /**
     * 描述 : 复制关键词
     * 作者 : Zoro.Zhu
     */
    'copyKeyword' : function () {
        let ids = "";
        $("input[name='check[]']:checked").each(function () {
            ids += $(this).val() + ",";
        });
        var input = document.getElementById("copy");
        input.value = ids; // 修改文本框的内容
        input.select(); // 选中文本
        document.execCommand("copy"); // 执行浏览器复制命令
        layer.msg('复制成功', {icon: 1});
    },

    /**
     * 描述 : 手动添加关键词
     * 作者 : Zoro.Zhu
     */
    'addKeyword' : function () {
        var keyword = window.prompt('请输入关键词', '');
        if (keyword === null || keyword === undefined || keyword === '') {
            return false;
        }
        var gtk = '320305.131321201';
        var sign = hash(keyword, gtk);
        var msg = layer.msg('努力中加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px',time: 0});
        $.ajax({
            url: ROOT_URL + "/?c=ctrl_wish_wishProduct&a=getKeywordInfo",
            type: "POST",
            data: {keyword: keyword, sign:sign, translator:'google',},
            success: function (result) {
                result = JSON.parse(result);
                if (result.state === 200) {
                    layer.close(msg);
                    var data = result.data;
                    var htmlStr = "<tr style=\"text-align: center\" class=\"change\" onclick=\"lookProductObj.onclickTr(this)\">";
                    htmlStr += "<td><input type=\"checkbox\" name=\"check[]\"  value='" + data.en + "' onclick=\"lookProductObj.keywordNumber()\"></td>";
                    htmlStr += "<td class=\"baiduFanyi\">" + data.en + "<br>" + data.cn + "</td>";
                    htmlStr += "<td>" + data.reach_level + "</td>";
                    htmlStr += "<td>" + data.competition_level + "</td>";
                    htmlStr += "<td>" + data.high_bid + "</td>";
                    htmlStr += "<td>" + data.itemCount + "</td>";
                    htmlStr += "<td>" + data.itemAmount + "</td>";
                    htmlStr += "<td>" + data.itemAmountRate + "%</td>";
                    htmlStr += "<td>$(" + data.avgWishPrice + "+" + data.avgFreightPrice + ")</td>";
                    htmlStr += "<td>" + data.itemCountRate + "%</td>";
                    htmlStr += "<td>" + data.amount + "+</td>";
                    htmlStr += "<td>" + data.rateNum + "</td>";
                    htmlStr += "<td>" + data.rateNum7 + "</td>";
                    htmlStr += "<td>" + data.rateNum7Rate + "%</td>";
                    htmlStr += "</tr>";
                    $('#addKeywordtr').before(htmlStr);
                    return false;
                } else {
                    layer.alert(result.info);
                    // parent.layer.closeAll();
                    return false;
                }
            }
        });
    }

};
$(function () {
    $('.baiduFanyi').each(function () {
        var content = $(this).text();
        var gtk = '320305.131321201';
        var sign = hash(content, gtk);
        var obj = $(this);
        $.ajax({
            url: ROOT_URL + "/serv/?c=rep_baidu&a=translate",
            type: "POST",
            async: true,
            data: {content : content, translator:'bing', to: 'cn', sign : sign},
            success: function (result) {
                result = JSON.parse(result);
                if(result.state === 1){
                    obj.html(content + '<br>' + result.tgt);
                }
            }
        });
    });
});

var hash = function (r, _gtk) {
    var o = r.length;
    o > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substr(-10, 10));
    var t = void 0, t = null !== C ? C : (C = _gtk || "") || "";
    for (var e = t.split("."), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) {
        var m = r.charCodeAt(g);
        128 > m ? d[f++] = m : (2048 > m ? d[f++] = m >> 6 | 192 : (55296 === (64512 & m) && g + 1 < r.length && 56320 === (64512 & r.charCodeAt(g + 1)) ? (m = 65536 + ((1023 & m) << 10) + (1023 & r.charCodeAt(++g)), d[f++] = m >> 18 | 240, d[f++] = m >> 12 & 63 | 128) : d[f++] = m >> 12 | 224, d[f++] = m >> 6 & 63 | 128), d[f++] = 63 & m | 128)
    }
    for (var S = h, u = "+-a^+6", l = "+-3^+b+-f", s = 0; s < d.length; s++) S += d[s], S = a(S, u);
    return S = a(S, l), S ^= i, 0 > S && (S = (2147483647 & S) + 2147483648), S %= 1e6, S.toString() + "." + (S ^ h)
}
